<?php

namespace app\api\service;

use think\facade\Db;
use think\facade\Queue;

class CouponService
{
    /**
     * 获取可兑换的优惠卷
     * */
    public static function getCouponList($store_id)
    {
       $couponList = Db::name('coupon')->field('id,coupon_name,icon,with_amount,used_amount,
                        expire_type,expire_day,start_time,end_time,quota,redeem_integral')
                        ->withAttr('icon',function ($value){
                            return request()->domain() . $value;
                        })->withAttr('start_time',function ($value){
                            if (empty($value)){
                                return  $value;
                            }
                           return date('Y-m-d H:i:s',$value);
                       })->withAttr('end_time',function ($value){
                           if (empty($value)){
                               return  $value;
                           }
                           return date('Y-m-d H:i:s',$value);
                       })
                        ->where(['store_id' => $store_id,'is_integral_redeem' => 1,'status' => 1])
                        ->select();

       return $couponList;
    }

    /**
     * 获取用户的优惠卷
     * */
    public static function getUserCouponList($user_id,$status)
    {
        $coupon_status = empty($status) ? 1 : $status;

        $couponList=Db::name('user_coupon')
                      ->field('coupon_id,coupon_type,with_amount,used_amount,coupon_name,coupon_icon,start_time,end_time,coupon_status')
                      ->withAttr('coupon_icon',function ($value){
                            return request()->domain() . $value;
                      })->withAttr('start_time',function ($value){
                          return date('Y-m-d H:i:s',$value);
                      })->withAttr('end_time',function ($value){
                         return date('Y-m-d H:i:s',$value);
                      })->withAttr('coupon_status',function ($value){
                          $status = [-1 => '已失效', 1 => '待使用' ,2  => '已失效'];
                          return $status[$value];
                    })->where(['user_id' => $user_id,'coupon_status' => $coupon_status])
                      ->order('end_time','asc')
                      ->select();

        return $couponList;
    }

    /**
     * 获取优惠券
     * */
    public static function findCoupon($coupon_id)
    {
        $coupon = Db::name('coupon')
                    ->field('id,coupon_type,coupon_name,with_amount,used_amount,icon,expire_type,expire_day,start_time,
                    end_time,quota,redeem_integral')
                    ->where('id',$coupon_id)
                    ->find();

        return $coupon;
    }

    /**
     * 批量添加用户优惠券
     * */
    public static function saveUserCoupon($dataSet)
    {
        $result = Db::name('user_coupon')->insertAll($dataSet);

        return $result;
    }


    /**
     * 兑换优惠券
     * */
    public static function redeemCoupon($user_id,$coupon)
    {

        if ($coupon['expire_type']==1){
            $start_time = time();
            $end_time = time() + (int)$coupon['expire_day'] * 24*3600;
        }

        if ($coupon['expire_type'] == 2){
            $start_time = $coupon['start_time'];
            $end_time = $coupon['end_time'];
        }

        Db::startTrans();

        try {
            Db::name('user_coupon')->insert([
                'user_id' => $user_id,
                'coupon_id' => $coupon['id'],
                'coupon_name' => $coupon['coupon_name'],
                'coupon_icon' => $coupon['icon'],
                'coupon_type' => $coupon['coupon_type'],
                'with_amount' => $coupon['with_amount'],
                'used_amount' => $coupon['used_amount'],
                'start_time' => $start_time,
                'end_time' => $end_time,
                'coupon_status' => 1,
                'receive_time' => time()
            ]);

            if ($coupon['quota'] !=-1){
                Db::name('coupon')->where('id',$coupon['id'])->dec('quota')->update();
            }

            Queue::push('app\store\job\Account@integral',[
                'user_id' => $user_id,
                'desc' => '积分兑换商品',
                'change_type'=> 2,
                'integral' => $coupon['redeem_integral']
            ],'userAccount');

            Db::commit();
            return  true;
        }catch (\Exception $e){
            Db::rollback();
            return  false;
        }
    }
}